package com.seven.ssa.controller;

import cn.dev33.satoken.annotation.SaCheckRole;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.seven.ssa.annotation.AuthCheck;
import com.seven.ssa.common.BaseResponse;
import com.seven.ssa.common.DeleteRequest;
import com.seven.ssa.common.ErrorCode;
import com.seven.ssa.common.ResultUtils;
import com.seven.ssa.exception.BusinessException;
import com.seven.ssa.model.dto.questionBankQuestion.QuestionBankQuestionAddRequest;
import com.seven.ssa.model.dto.questionBankQuestion.QuestionBankQuestionDeleteRequest;
import com.seven.ssa.model.dto.questionBankQuestion.QuestionBankQuestionQueryRequest;
import com.seven.ssa.model.dto.questionBankQuestion.QuestionBankQuestionUpdateRequest;
import com.seven.ssa.model.entity.QuestionBankQuestion;
import com.seven.ssa.model.entity.User;
import com.seven.ssa.service.QuestionBankQuestionService;
import com.seven.ssa.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 库目关系接口
 */
@RestController
@RequestMapping("/question/bank/question")
public class QuestionBankQuestionController {
    @Resource
    private QuestionBankQuestionService questionBankQuestionService;
    @Resource
    private UserService userService;

    /**
     * 创建库目关系
     *
     * @param questionBankQuestionAddRequest
     * @param request
     * @return
     */
    @PostMapping("/add")
    @SaCheckRole("admin")
    public BaseResponse<Long> addQuestionBankQuestion(@RequestBody QuestionBankQuestionAddRequest questionBankQuestionAddRequest, HttpServletRequest request) {
        if (questionBankQuestionAddRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        QuestionBankQuestion questionBankQuestion = new QuestionBankQuestion();
        BeanUtils.copyProperties(questionBankQuestionAddRequest, questionBankQuestion);
        // 设置userId列
        questionBankQuestion.setUserId(userService.getLoginUser(request).getId());
        // 校验【库目关系】
        questionBankQuestionService.validQuestionBankQuestion(questionBankQuestion);
        boolean result = questionBankQuestionService.save(questionBankQuestion);
        if (!result) {
            throw new BusinessException(ErrorCode.OPERATION_ERROR);
        }
        return ResultUtils.success(questionBankQuestion.getId());
    }

    /**
     * 删除库目关系
     *
     * @param questionBankQuestionDeleteRequest
     * @param request
     * @return
     */
    @PostMapping("/delete")
    @SaCheckRole("admin")
    public BaseResponse<Boolean> deleteQuestionBankQuestion(@RequestBody QuestionBankQuestionDeleteRequest questionBankQuestionDeleteRequest, HttpServletRequest request) {
        // 校验【库目关系删除请求】及其【题库id】【题目id】是否为空
        if (questionBankQuestionDeleteRequest == null
         || questionBankQuestionDeleteRequest.getQuestionId() == null
         || questionBankQuestionDeleteRequest.getQuestionBankId() == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        // 构造查询
        LambdaQueryWrapper<QuestionBankQuestion> lambdaQueryWrapper = Wrappers.lambdaQuery(QuestionBankQuestion.class)
                .eq(QuestionBankQuestion::getQuestionId, questionBankQuestionDeleteRequest.getQuestionId())
                .eq(QuestionBankQuestion::getQuestionBankId, questionBankQuestionDeleteRequest.getQuestionBankId());
        // 进行删除操作
        boolean b = questionBankQuestionService.remove(lambdaQueryWrapper);
        return ResultUtils.success(b);
    }

    /**
     * 更新库目关系
     *
     * @param questionBankQuestionUpdateRequest
     * @param request
     * @return
     */
    @PostMapping("/update")
    @SaCheckRole("admin")
    public BaseResponse<Boolean> updateQuestionBankQuestion(@RequestBody QuestionBankQuestionUpdateRequest questionBankQuestionUpdateRequest, HttpServletRequest request) {
        if (questionBankQuestionUpdateRequest == null || questionBankQuestionUpdateRequest.getId() == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        // 设置userId列
        questionBankQuestionUpdateRequest.setUserId(userService.getLoginUser(request).getId());
        QuestionBankQuestion questionBankQuestion = new QuestionBankQuestion();
        BeanUtils.copyProperties(questionBankQuestionUpdateRequest, questionBankQuestion);
        boolean result = questionBankQuestionService.updateById(questionBankQuestion);
        return ResultUtils.success(result);
    }
}
